--[[
模块名称：protoairdef
模块功能：协议convert
模块最后修改时间：2019.04.18
]]


module(...,package.seeall)

local slen,sbyte,ssub,sgsub,schar,srep,smatch,sgmatch = string.len,string.byte,string.sub,string.gsub,string.char,string.rep,string.match,string.gmatch
local sformat = string.format

local get


--[[
函数名：getstatus
功能  ：获取需要状态
参数  ：无
返回值：状态
]]
local function getstatus()
	local t = {}

	-- t.shake = (shkcnt > 0) and 1 or 0
	-- shkcnt = 0
	t.charger = chg.getcharger() and 1 or 0
	t.acc = acc.getflag() and 1 or 0
	t.gps = gps.isOpen() and 1 or 0
	t.sleep = pm.isSleep() and 1 or 0
	t.volt = chg.getvolt()	
	t.block = nvm.get("isBlock") and 1 or 0
	t.fly = 0
	t.poweroff = 0
	t.rest = 0
	t.dismantle = chg.getcharger() and 0 or 1
	return t
end

--[[
函数名：getgps
功能  ：获取GPS状态
参数  ：无
返回值：GPS经纬度，定位状态，速度
]]
local function getgpstat()
	local t = {}
	local Location = gps.getLocation()
	print("getgps:",Location.lngType,Location.lng,Location.latType,Location.lat,gps.getCourse(),gps.getSpeed())
	t.fix = gps.isFix()
	if gps.isFix() then
		t.lng,t.lat = Location.lng,Location.lat
	else
		t.lng,t.lat = manage.getlastgps()
	end
	t.lngType,t.latType = Location.lngType,Location.latType
	t.satenum = gps.getViewedSateCnt()
	-- t.fix = true
	if t.fix then
		t.lng,t.lat = t.lng or "",t.lat or ""
		t.cog = gps.getCourse()
		t.spd = gps.getSpeed()
		t.alt = gps.getAltitude()
		t.time = gps.getUtcTime()
		local dstZoneTime = common.timeZoneConvert(t.time.year,t.time.month,t.time.day,t.time.hour,t.time.min,t.time.sec,-8,0)
		t.time = dstZoneTime
	else
		t.lng,t.lat = "0.0","0.0"
		t.cog = 0
		t.spd = 0
		t.alt = 0
		tmp = misc.getClock()
		local dstZoneTime = tmp --common.timeZoneConvert(tmp.year,tmp.month,tmp.day,tmp.hour,tmp.min,tmp.sec,8,0)
        -- log.info("--zbb--linkout.gpsCb->",tmp.hour,dstZoneTime.hour)
        --dstZoneTime为{year=2018,month=1,day=2,hour=2,min=0,sec=0}
		t.time = dstZoneTime
	end
	return t
end

local tGet = {
	["VERSION"] = function() return _G.VERSION end,
	PROJECTID = function() return _G.PRJID end,
	PROJECT = function() return _G.PROJECT end,
	HEART = function() return nvm.get("heart") end,
	IMEI = misc.getImei,
	IMSI = sim.getImsi,
	ICCID = sim.getIccid,
	-- GPS = getgps,
	GPSTAT = getgpstat,
	RSSI = net.getRssi,
	CELLINFOEXT = net.getCellInfoExt,
	TA = net.getTa,
	STATUS = getstatus,
	GPSLEEP = function() return nvm.get("gpsleep") end,
	PARAVER = function() return nvm.get("paraver") end,
}
local function getf(id)
	assert(tGet[id] ~= nil,"getf nil id:" .. id)
	return tGet[id]()
end

local function enlnla(v,s)
	if not v then return string.toHex("FFFFFFFFFF") end
	
	local v1,v2 = smatch(s,"(%d+)%.(%d+)")
	if not v1 or not v2 then return string.toHex("FFFFFFFFFF") end

	if slen(v1) < 3 then v1 = srep("0",3-slen(v1)) .. v1 end

	return bcd(v1..v2,5)
end
-----------------------------------------------------------------------------------------------------------------------808

require"protoair808"
function setInitFunc(sfunc,sucfunc)
    return protoair808.setInitFunc(sfunc,sucfunc)
end

function init()
    return protoair808.init()
end

function reget(id)
    get = id
	protoair808.reget(id)
end

function heart()
	protoair808.heart()
end

function packAll(t,d)
    return protoair808.packAll(t,d)
end

function pack(t,d)
    return protoair808.pack(t,d)
end

function unpack(s)
    return protoair808.unpack(s)
end


function loc()
    local t = get("GPSTAT")
    local stat = get("STATUS")
    -- local shk,chgstat,chg,gpstat = false,chg.getcharger(),"NOT_CHARGE",false
    -- if chgstat then chg = "CHARGING" end
    -- if stat.gps == 1 then gpstat = true end	
    local adcr = adcv.read()
    -- local typstr = "DEV_TIMER_REPORT"
    -- if typ == QRYPOS then typstr = "SVR_QUERY_RSP" end 
    -- log.info("--zbb--protoairdef t:", t, stat, adcr)
    return protoair808.loc(t,stat,adcr)
end

function locbatch(msgs)
    return protoair808.locbatch(msgs)
end

------------------------------------------------------------------------------------------------------32960
-- require"protoair32960"
-- function setInitFunc(sfunc,sucfunc)
--     return protoair32960.setInitFunc(sfunc,sucfunc)
-- end

-- function init()
--     return protoair32960.init()
-- end

-- function reget(id)
--     get = id
-- 	protoair32960.reget(id)
-- end

-- function heart()
-- 	protoair32960.heart()
-- end

-- function packAll(t,d)
--     return protoair32960.packAll(t,d)
-- end

-- function pack(t,d)
--     return protoair32960.pack(t,d)
-- end

-- function unpack(s)
--     return protoair32960.unpack(s)
-- end


-- function loc()
--     local t = get("GPSTAT")
--     local stat = get("STATUS")
--     -- local shk,chgstat,chg,gpstat = false,chg.getcharger(),"NOT_CHARGE",false
--     -- if chgstat then chg = "CHARGING" end
--     -- if stat.gps == 1 then gpstat = true end	
--     local adcr = adcv.read()
--     -- local typstr = "DEV_TIMER_REPORT"
--     -- if typ == QRYPOS then typstr = "SVR_QUERY_RSP" end 
--     -- log.info("--zbb--protoairdef t:", t, stat, adcr)
--     return protoair32960.loc(t,stat,adcr)
-- end

-- function locbatch(msgs)
--     return protoair32960.locbatch(msgs)
-- end

----------------------------------------------------------------------------------------------------------end
reget(getf)

